package com.zzyy.study.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyy.study.entities.Result;
import com.zzyy.study.entities.User;
import com.zzyy.study.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @auther zzyy
 * @create 2025-03-15 16:45
 */
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Result insert(User user){
        int line = userMapper.insert(user);
        if(line > 0)
        {
            return Result.success(line);
        }

        return Result.fail(500,"操作数据库失败");
    }

    public Result delete(Integer id) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, id);
        int line = userMapper.delete(wrapper);
        if (line > 0)
        {
            return Result.success(line);
        }
        return Result.fail(500, "操作数据库失败");
    }

    public Result update(User user){
        int i = userMapper.updateById(user);
        if(i > 0)
        {
            return Result.success(i);
        }
        return Result.fail(500,"操作数据库失败");
    }

    public Result get(Integer id)
    {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,id);
        User user = userMapper.selectOne(wrapper);
        return Result.success(user);
    }

}

